@echo off
chcp 936 >nul
echo ====================================
echo    Microservices Launcher
echo ====================================
echo.

:: Check if Java is available
java -version >nul 2>&1
if %ERRORLEVEL% neq 0 (
    echo ERROR: Java is not installed or not in PATH
    pause
    exit /b 1
)

:: Check if Maven is available
mvn -version >nul 2>&1
if %ERRORLEVEL% neq 0 (
    echo ERROR: Maven is not installed or not in PATH
    pause
    exit /b 1
)

echo Environments verified successfully
echo.
echo Starting compilation process...
echo.

:: Compile all modules
set PROJECT_ROOT=F:\shopping

echo [1/6] Compiling common-module...
cd /d "%PROJECT_ROOT%\common-module"
mvn clean install -DskipTests >nul
if %ERRORLEVEL% neq 0 (
    echo ERROR: Failed to compile common-module
    pause
    exit /b 1
)
echo ✓ common-module compiled

echo [2/6] Compiling auth-service...
cd /d "%PROJECT_ROOT%\auth-service"
mvn clean package -DskipTests >nul
if %ERRORLEVEL% neq 0 (
    echo ERROR: Failed to compile auth-service
    pause
    exit /b 1
)
echo ✓ auth-service compiled

echo [3/6] Compiling user-service...
cd /d "%PROJECT_ROOT%\user-service"
mvn clean package -DskipTests >nul
if %ERRORLEVEL% neq 0 (
    echo ERROR: Failed to compile user-service
    pause
    exit /b 1
)
echo ✓ user-service compiled

echo [4/6] Compiling product-service...
cd /d "%PROJECT_ROOT%\product-service"
mvn clean package -DskipTests >nul
if %ERRORLEVEL% neq 0 (
    echo ERROR: Failed to compile product-service
    pause
    exit /b 1
)
echo ✓ product-service compiled

echo [5/6] Compiling order-service...
cd /d "%PROJECT_ROOT%\order-service"
mvn clean package -DskipTests >nul
if %ERRORLEVEL% neq 0 (
    echo ERROR: Failed to compile order-service
    pause
    exit /b 1
)
echo ✓ order-service compiled

echo [6/6] Compiling gateway-service...
cd /d "%PROJECT_ROOT%\gateway-service"
mvn clean package -DskipTests >nul
if %ERRORLEVEL% neq 0 (
    echo ERROR: Failed to compile gateway-service
    pause
    exit /b 1
)
echo ✓ gateway-service compiled

echo.
echo All services compiled successfully!
echo.
echo Starting microservices...
echo.

:: Start services with 5 second intervals
echo Starting auth-service (port 8084)...
start "auth-service" cmd /k "cd /d "%PROJECT_ROOT%\auth-service" && java -jar target\*.jar"
timeout /t 5 /nobreak >nul

echo Starting user-service (port 8081)...
start "user-service" cmd /k "cd /d "%PROJECT_ROOT%\user-service" && java -jar target\*.jar"
timeout /t 5 /nobreak >nul

echo Starting product-service (port 8082)...
start "product-service" cmd /k "cd /d "%PROJECT_ROOT%\product-service" && java -jar target\*.jar"
timeout /t 5 /nobreak >nul

echo Starting order-service (port 8083)...
start "order-service" cmd /k "cd /d "%PROJECT_ROOT%\order-service" && java -jar target\*.jar"
timeout /t 5 /nobreak >nul

echo Starting gateway-service (port 8080)...
start "gateway-service" cmd /k "cd /d "%PROJECT_ROOT%\gateway-service" && java -jar target\*.jar"

echo.
echo ====================================
echo    All services started!
echo ====================================
echo.
echo Service ports:
echo - Gateway:      8080
echo - Auth:         8084
echo - User:         8081
echo - Product:      8082
echo - Order:        8083
echo.
echo Nacos Console: http://192.168.152.48:8848/nacos
echo.
echo Press any key to close this window (services will continue running)...
pause >nul